﻿Imports System.IO
Imports Runa.Common.Helper
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports System.IO.IsolatedStorage

Namespace Algorithm

    Public NotInheritable Class Image

        Public Shared Function Scale(ByVal originalImg As Graphics.Texture2D, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Graphics.Texture2D
            Using ms As New MemoryStream()
                originalImg.SaveAsPng(ms, originalImg.Width, originalImg.Height)
#If Not NETFX_CORE Then
                Return Texture2D.FromStream(originalImg.GraphicsDevice, ms, width, height, aspectRatio)
#Else
                Return Texture2D.FromStream(originalImg.GraphicsDevice, ms)
#End If
            End Using
        End Function

#If WINDOWS_PHONE Then
        Public Shared Function Scale(ByVal sourceStream As Stream, ByVal originalImg As String, ByVal newFileName As String, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Boolean
            Try
                Dim oriImageObject = Texture2D.FromStream(Nothing, sourceStream, width, height, aspectRatio)
                Using targetStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(newFileName, FileMode.Create)
                    oriImageObject.SaveAsPng(targetStream, width, height)
                    targetStream.Close()
                End Using
#ElseIf NETFX_CORE Then
        Public Shared Function Scale(ByVal sourceStream As Stream, ByVal originalImg As String, ByVal newFileName As String, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Boolean
            Try
                Dim oriImageObject = Texture2D.FromStream(Nothing, sourceStream)
                Using targetStream = IsolatedStorageFile.GetUserStoreForAssembly().OpenFile(newFileName, System.IO.FileMode.Create)
                    oriImageObject.SaveAsPng(targetStream, width, height)
                    targetStream.Close()
                End Using
#Else
        Public Shared Function Scale(ByVal sourceStream As Stream, ByVal originalImg As String, ByVal newFileName As String, ByVal width As Integer, ByVal height As Integer, Optional ByVal aspectRatio As Boolean = False) As Boolean
            Try
                Dim oriImageObject = Texture2D.FromStream(Nothing, sourceStream, width, height, aspectRatio)
                Using targetStream = IsolatedStorageFile.GetUserStoreForAssembly().OpenFile(newFileName, FileMode.Create)
                    oriImageObject.SaveAsPng(targetStream, width, height)
                    targetStream.Close()
                End Using
#End If
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Shared Function GetAlphaTexture(ByVal g As GraphicsDevice, ByVal r As Vector2) As Texture2D
            Dim data(0) As Color
            Array.Resize(data, CInt(r.X) * CInt(r.Y))
            For Each c In data
                c = New Color(255, 255, 255, 255)
            Next
            Dim t As New Texture2D(g, CInt(r.X), CInt(r.Y))
            t.SetData(data)
            Return t
        End Function

        Public Shared Function GetGradientColorTexture(ByVal g As GraphicsDevice, ByVal r As Vector2, ByVal startColor As Color, ByVal endColor As Color, Optional ByVal divisor As Integer = 1) As Texture2D
            Dim data(0) As Color
            Array.Resize(data, CInt(r.X) * CInt(r.Y))
            Dim colorWidth = endColor.ToVector4() - startColor.ToVector4()
            Dim c As New Color(startColor.ToVector4() + 0 * (colorWidth / divisor))
            For y As Integer = 0 To CInt(r.Y) - 1
                If y Mod divisor = 0 Then
                    c = New Color(startColor.ToVector4() + y * (colorWidth / divisor))
                End If
                For x As Integer = 0 To CInt(r.X) - 1
                    Dim Index = y * CInt(r.X) + x
                    data(Index) = c
                Next
            Next
            Dim t As New Texture2D(g, CInt(r.X), CInt(r.Y))
            t.SetData(data)
            Return t
        End Function

    End Class

End Namespace